////////////////////////////////////////////////////////////////////////////////
// Copyright 2007 CORE Solutions Limited
// All rights reserved.
// PROPRIETARY AND CONFIDENTIAL INFORMATION.
// Use is subject to license terms.
//
// FUNCTION ID      :   POPUP_CATALOG_TREE
// AUTHOR           :   Saint Peng 
// DATE CREATE      :   2007/04/05
// DESCRIPTION      :   a ACTION file for the function, POPUP_CATALOG_TREE
//                  :   
//                  :   
//                  :   
//
// MODIFICATION HISTORY:
// DATE         AUTHOR          VERSION NO.         COMMENT
// -----------  --------------  ------------------  -------------------
// 2007/04/05   Saint Peng      1.0                 source is generated.
//
//
//
////////////////////////////////////////////////////////////////////////////////

package com.erp.app.catalog.catalogTreePopup.web;

import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.erp.app.catalog.catalogTreePopup.criteria.PopupCatalogTreeSrchCriteria;
import com.erp.app.catalog.catalogTreePopup.vo.PopupCatalogTreeVo;
import com.erp.app.common.ProjectConstants;
import com.erp.app.util.web.WebUtil;
import com.erp.framework.controller.action.SimplePageEditStyleAction;
import com.erp.framework.util.CommonUtil;
import com.erp.framework.util.logger.Logger;
import com.erp.framework.util.user.User;

/**
* A <strong>Page Edit Action</strong> is an adapter between the contents of an incoming
* HTTP request and the corresponding business logic that should be executed to
* process this request.  The controller (ActionServlet) will select an
* appropriate Action for each request.
* The main purpose for this action class is to handle the one-page document events
* from the web browsers.
*
* @author          Saint Peng 
* @version 1.0     2007/04/05
* @since           FrameWork 1.0
*
*/
public class PopupCatalogTreeAction extends SimplePageEditStyleAction {

	////////////////////////////////////////////////////////////////////////////////
	// PRIVATE VARIABLES DEFINITION
	////////////////////////////////////////////////////////////////////////////////

//	The following classes can be used to access common utilities,
//	delegate methods, and common validation functions.
//	private PopupCatalogTreeCommonUtil util = new PopupCatalogTreeCommonUtil();
//	private PopupCatalogTreeCommonDelegate dg = new PopupCatalogTreeCommonDelegate();
	private PopupCatalogTreeCommonValidator validator = new PopupCatalogTreeCommonValidator();


	////////////////////////////////////////////////////////////////////////////////
	// GLOBAL VARIABLES DEFINITION
	////////////////////////////////////////////////////////////////////////////////



	////////////////////////////////////////////////////////////////////////////////
	// CONSTANTS DEFINITION
	////////////////////////////////////////////////////////////////////////////////
	public static final String STATUS_SELECT_ALL = ACTION_SELECT_ALL;
	public static final String ACTION_SELECT_FINISH = "selectFinish";
	public static final String STATUS_SELECT_FINISH = ACTION_SELECT_FINISH;

	//public static final String ACTION_DO_SOMETHING = "actionDoSomething";
	//public static final String STATUS_DO_SOMETHING = "statusDoSomething";


	////////////////////////////////////////////////////////////////////////////////
	// LOGICAL AND BUSINESS METHODS
	////////////////////////////////////////////////////////////////////////////////

	public ActionForward handlePageEvent(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		HttpSession session = request.getSession(false);
		PopupCatalogTreeForm form = (PopupCatalogTreeForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {
			if (!isHandled(request) && action.equalsIgnoreCase(ACTION_SELECT_FINISH)) {
				actionForward = processActionSelectFinish(mapping, formIn, request, response, errors);
				setHandled(request);
				saveToken(request);
			}

		} catch (Exception e) {
			Logger.logExceptions(this, e);
			errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("exception.error", e.getMessage()));
		}

		return actionForward;
	}
	
	private ActionForward processActionSelectFinish(ActionMapping mapping, ActionForm formIn, HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {
    	HttpSession session = request.getSession(false);
    	PopupCatalogTreeForm form = (PopupCatalogTreeForm) formIn;
        ActionForward actionForward = new ActionForward(mapping.getInput());
        User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);
        String str = (String)CommonUtil.nullToString(form.getPageEventSrc());
        ArrayList rows = form.getMultiRows();
        ArrayList rowList = new ArrayList();
        HashMap nodeMap = new HashMap();
        String nodeIdList[] = CommonUtil.split(str, ",");
        System.out.println("%%%%%%%%%%%%%%%% nodeStr = " + str);
        if(nodeIdList != null && nodeIdList.length > 0) {
        	for(int i=0; i < nodeIdList.length; i++) {
        		nodeMap.put(String.valueOf(i), nodeIdList[i]);
        		System.out.println("%%%%%%%%%%%%%%%% split node id = " + nodeIdList[i]);
        	}
        }
        if(rows != null && rows.size() > 0) {
        	for(int i = 0; i < rows.size(); i++) {
        		PopupCatalogTreeRow row = (PopupCatalogTreeRow)rows.get(i);
        		if(nodeMap.containsValue(row.getNodeId())) {
        			rowList.add(row);
        		}
        	}
        }
        if(session.getAttribute(ProjectConstants.POP_UP_CATALOG_TREE_NODE_ID_SET) != null) {
        	session.removeAttribute(ProjectConstants.POP_UP_CATALOG_TREE_NODE_ID_SET);
        }
        session.setAttribute(ProjectConstants.POP_UP_CATALOG_TREE_NODE_ID_SET, rowList);
        return actionForward;
	}

	protected ActionForward processOtherStatus(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response,
		ActionErrors errors, String rb_status) {

		HttpSession session = request.getSession(false);
		PopupCatalogTreeForm form = (PopupCatalogTreeForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {
//			if (rb_status.equalsIgnoreCase(STATUS_DO_SOMETHING)) {
//				return processStatusDoSomething(mapping,  formIn,  request, response,  errors,  rb_status);
//			}
		} catch (Exception e) {
			Logger.logExceptions(this, e);
			errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("exception.error", e.getMessage()));
		}

		return actionForward;
	}

	//uncomment the following code and implement the method if necessary
	/*
	protected ActionForward processActionDoSomething(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		HttpSession session = request.getSession(false);
		PopupCatalogTreeForm form = (PopupCatalogTreeForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try{
			System.out.println("You are in processActionDoSomething().");
		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}

		return actionForward;
	}
	*/

	//uncomment the following code and implement the method if necessary
	/*
	protected ActionForward processStatusDoSomething(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response,
		ActionErrors errors, String rb_status) {

		HttpSession session = request.getSession(false);
		PopupCatalogTreeForm form = (PopupCatalogTreeForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try{
			System.out.println("You are in processStatusDoSomething().");
		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}

		return actionForward;
	}
	*/

	////////////////////////////////////////////////////////////////////////////////
	// END OF LOGICAL AND BUSINESS METHODS
	////////////////////////////////////////////////////////////////////////////////


	////////////////////////////////////////////////////////////////////////////////
	// OVERRIDES PARENT'S OPTIONAL METHODS
	////////////////////////////////////////////////////////////////////////////////

	//the following method is only to be overwritten by sub-class if intended
	//to set the aut-search on-load to on/off
	protected boolean getLoadOnEnter() {

		//auto-search when load
		return true;
	}

	//the following method is only to be overwritten by sub-class if intended
	//to set the clearing search result sets on-load to on/off
	protected boolean getResetOnEnter() {

		//reset last searched results when load 
		return true;
	}

	protected void initAction(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		super.initAction(mapping, formIn, request, response, errors);

		HttpSession session = request.getSession(false);
		PopupCatalogTreeForm form = (PopupCatalogTreeForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {
//			HashMap errMsgMap = WebUtil.getModuleErrorMessages(editingUser.getCompanyCode(),
//				DBMENU_GENERAL_MENU, form.getModuleName(), form.getSysLangCode(), DBMENU_OBJECT_TYPE_ERR_MSG);
//			form.setErrMsgMap(errMsgMap);

		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}
	}

	protected void initForm(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		super.initForm(mapping, formIn, request, response, errors);

		HttpSession session = request.getSession(false);
		PopupCatalogTreeForm form = (PopupCatalogTreeForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {

		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}
	}

	protected void initPage(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		super.initPage(mapping, formIn, request, response, errors);

		HttpSession session = request.getSession(false);
		PopupCatalogTreeForm form = (PopupCatalogTreeForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {
			
			if(session.getAttribute(ProjectConstants.POP_UP_CATALOG_TREE_CHECKBOX_PARAMS) != null){
				HashMap params = (HashMap) session.getAttribute(ProjectConstants.POP_UP_CATALOG_TREE_CHECKBOX_PARAMS);
				if(CommonUtil.nullToString(params.get("trueSetParent")).equals(ProjectConstants.GENERIC_YES)){
					form.setTrueSetParent(true);
				}
				if(CommonUtil.nullToString(params.get("falseSetParent")).equals(ProjectConstants.GENERIC_YES)){
					form.setFalseSetParent(true);
				}
				if(CommonUtil.nullToString(params.get("trueSetChild")).equals(ProjectConstants.GENERIC_YES)){
					form.setTrueSetChild(true);
				}
				if(CommonUtil.nullToString(params.get("falseSetChild")).equals(ProjectConstants.GENERIC_YES)){
					form.setFalseSetChild(true);
				}
				if(CommonUtil.nullToString(params.get("needChild")).equals(ProjectConstants.GENERIC_YES)){
					form.setNeedChild(true);
				}
				session.removeAttribute(ProjectConstants.POP_UP_CATALOG_TREE_CHECKBOX_PARAMS);
			}
		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}
	}

	protected void beforeSearch(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		HttpSession session = request.getSession(false);
		PopupCatalogTreeForm form = (PopupCatalogTreeForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {

		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}
	}

	protected void afterSearch(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		HttpSession session = request.getSession(false);
		PopupCatalogTreeForm form = (PopupCatalogTreeForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {

		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}
	}

	protected void beforeSave(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		HttpSession session = request.getSession(false);
		PopupCatalogTreeForm form = (PopupCatalogTreeForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {

		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}
	}

	protected void afterSave(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		HttpSession session = request.getSession(false);
		PopupCatalogTreeForm form = (PopupCatalogTreeForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {

		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}
	}

	protected void beforeRefresh(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		HttpSession session = request.getSession(false);
		PopupCatalogTreeForm form = (PopupCatalogTreeForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {

		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}
	}

	protected void afterRefresh(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		HttpSession session = request.getSession(false);
		PopupCatalogTreeForm form = (PopupCatalogTreeForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {

		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}
	}

	protected void beforeAddRow(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		HttpSession session = request.getSession(false);
		PopupCatalogTreeForm form = (PopupCatalogTreeForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {

		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}
	}

	protected void afterAddRow(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		HttpSession session = request.getSession(false);
		PopupCatalogTreeForm form = (PopupCatalogTreeForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {
			ArrayList rows = form.getMultiRows();
			if (rows != null && rows.size() > 0) {
				PopupCatalogTreeRow row = (PopupCatalogTreeRow) rows.get(rows.size() - 1);
			}
		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}
	}

	protected void beforeDeleteRow(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		HttpSession session = request.getSession(false);
		PopupCatalogTreeForm form = (PopupCatalogTreeForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {

		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}
	}

	protected void afterDeleteRow(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		HttpSession session = request.getSession(false);
		PopupCatalogTreeForm form = (PopupCatalogTreeForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {

		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}
	}

	protected String getSimpleSrchMenuId() {

		//TODO: Add the following line to ProjectMenuConstants.java
		//public static final String MOD_POPUP_CATALOG_TREE = "MOD_POPUP_CATALOG_TREE";
		return ProjectConstants.MOD_POPUP_CATALOG_TREE;
	}

	////////////////////////////////////////////////////////////////////////////////
	// END OF OPTIONAL METHODS
	////////////////////////////////////////////////////////////////////////////////


	////////////////////////////////////////////////////////////////////////////////
	// IMPLEMENTS PARENT'S ABSTRACT METHODS
	////////////////////////////////////////////////////////////////////////////////

	protected String getModuleName(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request,HttpServletResponse response) {

		//TODO: Add the following line to ProjectMenuConstants.java
		//public static final String MOD_POPUP_CATALOG_TREE = "MOD_POPUP_CATALOG_TREE";
		return ProjectConstants.MOD_POPUP_CATALOG_TREE;
	}

	protected String getFunctionId() {

		//struts-config xxxxx.do
		return "popupCatalogTree.do";
	}

	protected void setFormType(ActionMapping mapping, ActionForm formIn, HttpServletRequest request,
		 HttpServletResponse response, ActionErrors errors) {

		PopupCatalogTreeForm form = (PopupCatalogTreeForm) formIn;
		form.setFormType(ProjectConstants.FORM_TYPE_PAGE_EDIT);
	}

	protected String getMasterDelegateName() {

		//the corresponding delegate session name
		return "popupCatalogTreeDelegate";
	}

	protected Class getMasterFormClass() {

		//the corresponding form class
		return PopupCatalogTreeForm.class;
	}

	protected Class getMasterVOClass() {

		//corresponding master vo class
		return PopupCatalogTreeVo.class;
	}

	protected Class getDetailVOClass() {

		//corresponding detail vo class
		return PopupCatalogTreeVo.class;
	}

	protected Class getDetailFormClass() {
		//corresponding detail row class
		return PopupCatalogTreeRow.class;
	}

	protected String getDetailFormSessionName() {

		//corresponding detail form session name
		return "popupCatalogTreeForm";
	}

	protected Class getSearchCriteriaClass() {

		//corresponding searching criteria class
		return PopupCatalogTreeSrchCriteria.class;
	}

	protected void setOtherCriteria(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response,
		ActionErrors errors, Object criteria) {

		HttpSession session = request.getSession(false);
		User user = (User) session.getAttribute(ProjectConstants.SESSION_USER);
		PopupCatalogTreeSrchCriteria crit = (PopupCatalogTreeSrchCriteria) criteria;
		String companyCode = WebUtil.getAppropriateCompanyCode(session, null);

		crit.setSrchCompanyCode(companyCode);
		crit.setSearchResult(true);
	}

	////////////////////////////////////////////////////////////////////////////////
	// END OF ABSTRACT METHODS
	////////////////////////////////////////////////////////////////////////////////
}
